#ifndef MAINLAYER_H_
#define MAINLAYER_H_

#include "CShip.h"
#include "CEnemyGenerator.h"
#include "CEnemy.h"
#include "HudLayer.h"
#include "cocos2d.h"
#include "CDebugDrawGLES.h"
/**
 * This layer controls the ship movement, enemy generation.
 */
class MainLayer:
	public cocos2d::CCLayer,
	public b2ContactListener
{
public:
	enum EMainLayerObjectTags
	{
		shipTag,
		bulletTag,
		shieldTag,
		enemyTag
	};
	MainLayer();
	~MainLayer();
	bool init();
	void setHudLayer(HudLayer* hudLayer);
	void draw();
	void didAccelerate(cocos2d::CCAcceleration* pAccelerationValue);
	void onEnter();
	void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	void update(float dt);
	void updateScore(float points = 0.0f);
	void setCalibrationValues(b2Vec3 CalibrationValues){ __calibrationValues = CalibrationValues;}
	void updateHealthPoints(float points);
	void updateShieldPoints(float points);
	void updateB2Bodies();
	void BeginContact(b2Contact* pContact);
	void EndContact(b2Contact* pContact);
	void PreSolve(b2Contact* pContact, const b2Manifold* pOldManifold);
	void PostSolve(b2Contact* pContact, const b2ContactImpulse* pImpulse);
	void changeGun();
	void toggleAutofire();
	void changeDrawDebugMode();
	void fire(float dt);
	bool NotCheckCollision(CAnimatedObject* obj1,CAnimatedObject* obj2);
	// implement the "static node()" method manually
	NODE_FUNC(MainLayer);

private:
	/**
	 * Every sprite in the MainScene is a child of the __pSpriteSheet.
	 * It is initialized with the MainScene.png file as the parameter.
	 */
	cocos2d::CCSpriteBatchNode* 		__pSpriteSheet;

	/**
	 * The layer displaying the score.
	 */
	HudLayer*							__pHudLayer;

	/**
	 * The main game character controlled by the player.
	 */
	CShip*			 					__pShip;

	/**
	 * Accelerometer values read when the user pressed the "Start" button.
	 */
	b2Vec3								__calibrationValues;


	/**
	 * The current screen size.
	 */
	cocos2d::CCSize 					__winSize;

	/**
	 * Array holding the last values of acceleration on the Z axis.
	 * It is used to calculate the current average acceleration on the Z axis.
	 * This is used to determine if a barrel roll should be performed.
	 */
	float*								__zAccelArray;

	int									__zAccelArrayLength;
	int 								__currentZAccelArrayIndex;

	/**
	 * Generates enemies of different types and sizes.
	 */
	CEnemyGenerator*						__pEnemyGenerator;

	/**
	 * Holds currently active enemies.
	 */
	cocos2d::CCArray*	__pEnemies;


	/**
	 * Player's current score.
	 */
	int									__score;

	/**
	 * The Box2D world that will hold all the bodies.
	 */
	b2World*				 			__pWorld;

	/**
	 * If true, the ship will fire bullets automatically in certain intervals.
	 */
	bool								__autofire;

	/**
	 * If true, will draw debug for every box2d object.
	 */
	bool								_isDrawBoxDebug;

	// debug draw
	CDebugDrawGLES* _debugDraw;

	float getAvgZAccel();
	void cleanEnemies();
	void updateEnemies();
	void endGame();
};

#endif /* MAINLAYER_H_ */
